"""

@project: datest

@author: MZM

@file: debugtalk.py

@ide: PyCharm

@time: 2021/3/4 16:14

@desc：测试用例进行发送请求前后对请求和响应的默认调用方法，可添加在前台定义的请求前置方法

"""



import json
import random

import time

from urllib.parse import quote

import hashlib

import logging

import hmac

import base64

from database import Database



import allure



def randname():
    return 'test' + str(random.randint(9999,99999))

def wait(seconds):
    time.sleep(seconds)

def prerequest(request):

    @allure.step('请求参数')

    def newpost(url, params, data,jsondata,headers):

        logging.debug(f'url:{url},headers:{headers},params:{params},data:{str(data)},json:{json.dumps(jsondata)}')

    newpost(url=request['url'], headers=request['headers'],data = request['data'], params=request['params'],jsondata=request['req_json'])

def updatepic(request):
    data = request['data']
    data.fields.update({"images": "http://127.0.0.1/shopxo/public/static/upload/images/goods/2019/01/14/1547455601898622.jpg","specifications_price[]": 99, "specifications_weight[]": 1, "specifications_coding[]": "", "specifications_barcode[]": "", "specifications_original_price[]": "", "specifications_extends[]": "", "photo[]": "http://127.0.0.1/shopxo/public/static/upload/images/goods/2020/03/05/1583374531934359.jpeg"})


def afterresponse(response):
    response.extdata = {}

    @allure.step('响应数据')

    def getresp(status_code, responsedata):

        '''
        测试用例进行发送请求后响应对象的默认调用方法
        '''

        logging.debug("响应码：" + str(status_code))

        logging.debug("响应数据：" + responsedata)

    getresp(response.status_code, response.text)

    allure.attach(response.text, 'response.txt', allure.attachment_type.TEXT)

def get_cookies(response):
    response.extdata['sessid'] = response.resp_obj.cookies.get('PHPSESSID')
    print(response.extdata['sessid'])

def get_sql_data(sql,param=None,key='id'):
    with Database() as db:
        result = db.fetch_one(sql,param)[key]
    return str(result)

# if __name__ == "__main__":
#     print(get_sql_data('SELECT * FROM shopxo.s_admin where username = %s','sdasd','id'))